package yxy.game.pm2.command.capturestrategicpoints;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CaptureStrategicPointsMessage;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsModule;

/**
 * @author wrs
 * @descrition 斩关夺隘观星
 * @since 2022-08-17
 */
@MessageType(proto = Command.CaptureStrategicPointsStars, classtype = CaptureStrategicPointsMessage.CaptureStrategicPointsStarsRequest.class)
public class CMD_CaptureStrategicPointsStars extends Command<CaptureStrategicPointsMessage.CaptureStrategicPointsStarsRequest> {

    @Override
    public void execute(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsStarsRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CaptureStrategicPointsMessage.CaptureStrategicPointsStarsResponse.Builder response = CaptureStrategicPointsMessage.CaptureStrategicPointsStarsResponse.newBuilder();

        try {
            CaptureStrategicPointsModule module = CaptureStrategicPointsModule.get(playerCode);

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            //todo 返回
            int starsLevel = module.getStarsLevel();
            int totalStars = module.getTotalStars();
            response.setDataId(starsLevel);
            response.setTotalStar(totalStars);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsStarsResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
